﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace PIMS.Utils
{
    internal class IPTools
    {
        /// <summary>
        /// 判断指定的IP是否在线（带超时设定功能）
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIPOnLine(string ip)
        {
            try
            {
                bool online = false; //是否在线
                Ping ping = new Ping();
                PingOptions ops = new PingOptions();
                ops.DontFragment = true;
                string d = "test data";
                byte[] buf = Encoding.ASCII.GetBytes(d);
                int timeout = 1000;
                PingReply pingReply = ping.Send(ip, timeout, buf, ops);
                if (pingReply.Status == IPStatus.Success)
                {
                    online = true;
                }
                return online;
            }
            catch
            {
                return false;
            }
        }
    }
}
